/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package log2excel.ExcelManager;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 *
 * @author aegish
 */
public class CreateExcel {
    private String filename;
    private WritableWorkbook workbook;
    private WritableSheet sheet;
    public CreateExcel(String filename) {
        try {
            this.filename = filename;
            WorkbookSettings ws = new WorkbookSettings();
            ws.setLocale(Locale.ENGLISH);
            workbook = Workbook.createWorkbook(new File(this.filename), ws);
        } catch (IOException ex) {
            Logger.getLogger(CreateExcel.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    public void createSheet(String sheetname, int index){
        sheet = workbook.createSheet(sheetname, index);
    }
    public void write_close(){
        try {
            workbook.write();
            workbook.close();
        } catch (WriteException ex) {
            Logger.getLogger(CreateExcel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CreateExcel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void putRow(int startCellrow, int startCellcolumn, ArrayList arr,WritableFont font, String label){
        try {
            int withlabel = 0;
            Label l;
            if (!(label == null || label.length() == 0)){
                WritableCellFormat cf = new WritableCellFormat(font);
                l = new Label(startCellcolumn, startCellrow, label, cf);
                sheet.addCell(l);
                withlabel = 1;
            }
            for (int i = 0; i < arr.size(); i++){
                l = new Label(i + startCellcolumn + withlabel, startCellrow , arr.get(i).toString());
                sheet.addCell(l);
            }
        } catch (WriteException ex) {
            Logger.getLogger(CreateExcel.class.getName()).log(Level.SEVERE, null, ex);
        }
    };
    public void putCollum(int startCellrow, int startCellcolumn, ArrayList arr,WritableFont font, String label){
        try {
            int withlabel = 0;
            Label l;
            if (!(label == null || label.length() == 0)){
                WritableCellFormat cf = new WritableCellFormat(font);
                l = new Label(startCellcolumn, startCellrow, label, cf);
                sheet.addCell(l);
                withlabel = 1;
            }
            for (int i = 0; i < arr.size(); i++){
                l = new Label(startCellcolumn, i + startCellrow + withlabel, arr.get(i).toString());
                sheet.addCell(l);
            }
        } catch (WriteException ex) {
            Logger.getLogger(CreateExcel.class.getName()).log(Level.SEVERE, null, ex);
        }
    };
    public void putCell(int startCellrow, int startCellcolumn, Object val, WritableFont font){
        try {
            WritableCellFormat cf = new WritableCellFormat(font);
            Label l = new Label(startCellcolumn, startCellrow, val.toString(), cf);
            sheet.addCell(l);
        } catch (WriteException ex) {
            Logger.getLogger(CreateExcel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public OpenExcel openReadableWorkbook(){
        write_close();
        return new OpenExcel(filename);
    }
}
